using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using IGlobalist.Chat.Engine;
using IGlobalist.Chat.Engine.Common;

using System.Web.Script.Services;
using System.Web.Script;
using System.Web.Services;

namespace IGlobalist.Cms.Chat
{
    public partial class Private : ChatBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtMsg.Attributes.Add("onkeypress", "return APrivate.clickButton(event,'btn')");
            if (!IsPostBack)
            {
                string roomID = Request.QueryString["rid"];
                Room room = GetChatRoom(roomID);
                string receiver = Request.QueryString["receiver"];
                hdnRoomID.Value = roomID;
                hdnReceiver.Value = receiver;
                lblCurrentUser.Text = receiver;
                lblRoomName.Text = room.Name;
                this.Title = receiver;
            }
        }

        #region Script Callback functions

        /// <summary>
        /// This function is called from the client script 
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="roomID"></param>
        /// <returns></returns>
        [WebMethod]
        static public bool SendMessage(string msg, string roomID, string receiver)
        {
            try
            {
                Room room = IGlobalist.Chat.Engine.ChatEngine.GetRoom(roomID);

                if (room != null)
                {
                    room.SendMessage(msg, GetUserID(room.Administrator.UserName, room.ID), receiver, MsgType.Private);
                }

            }
            catch (Exception ex)
            {

            }
            return true;
        }




        /// <summary>
        /// This function is called from the client when the user is about to leave the room
        /// </summary>
        /// <param name="otherUser"></param>
        /// <returns></returns>
        [WebMethod]
        static public string LeaveRoom(string roomID)
        {
            try
            {
                Room room = IGlobalist.Chat.Engine.ChatEngine.GetRoom(roomID);
                if (room != null)
                {

                    room.LeaveRoom(GetUserID(room.Administrator.UserName, room.ID));
                }
                //room.LeaveRoom(HttpContext.Current.Session["UserName"].ToString());
            }
            catch (Exception ex)
            {

            }
            return "";
        }


        #endregion
    }
}
